package com.shou.lifecollege.framework.Security.handle;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.shou.lifecollege.framework.Common.constant.HttpStatus;
import com.shou.lifecollege.framework.Common.result.RequestResult;
import com.shou.lifecollege.framework.Common.util.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @Author: luotian
 * @Date: 2022/11/24 17:17
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        int code= HttpStatus.UNAUTHORIZED;
        String msg= StrUtil.format("请求访问：{}，认证失败，无法访问系统资源",request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(RequestResult.error(code,msg)));

    }
}
